﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;


public partial class Police_AcceptTransfer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindAcceptCount();
            BindTransfer();
        }
    }

    private void BindTransfer()
    {
        gvTransfer.DataSource = TransferSoldier.GetTransferListByDepartmentID(Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4) + "000");
        gvTransfer.DataBind();
    }

    private void BindAcceptCount()
    {
        //具备本级调动权限
        //if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400103") || Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "400104"))
        //{
            string DepartmentID=Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4) + "000";

            int Count= TransferSoldier.NeedAcceptedCount(DepartmentID);
            gvTrasferStat.DataSource= TransferSoldier.GetTransfersByWhere(" toUnitID='"+DepartmentID+"' and acceptorID is null and CheckInDate is null", Count, 0);
            gvTrasferStat.DataBind();
            //gvTrasferStat.DataSource = Department.GetList("DepartmentID='" + Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID.Substring(0, 4) + "000'");
            //gvTrasferStat.DataBind();
        //}
        //else
        //{
        //    Response.Write("<script>alert('没有权限！');history.back(0);</script>");

        //}
    }

    public string GetSoldierName(object obj)
    {
        if (obj == null)
            return "";

        Yipusoft.SoldierArchives.BLL.Soldier soldier = new Soldier();
        if (string.IsNullOrEmpty(obj.ToString()))
            return "";

        Yipusoft.SoldierArchives.Model.SoldierArchiveInfo info= soldier.GetSoldierInfoByID(obj.ToString());
        if (info == null)
            return "";

        return info.SoldierName;

    }
    protected void gvTrasferStat_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Display the company name in italics.
            //int count = TransferSoldier.NeedAcceptedCount(gvTrasferStat.DataKeys[e.Row.RowIndex].Value.ToString());
            //e.Row.Cells[2].Text = count.ToString();
            //LinkButton btn = e.Row.Cells[3].FindControl("btnAccept") as LinkButton;
            //btn.Visible = count == 0 ? false : true;

            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (gvTrasferStat.PageIndex * gvTrasferStat.PageSize + e.Row.RowIndex + 1).ToString();
            ////当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");
        }


    }


    protected void gvTransfer_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string TID = gvTransfer.DataKeys[e.NewSelectedIndex].Values["TransferID"].ToString();
        string ID = gvTransfer.DataKeys[e.NewSelectedIndex].Values["SoldierID"].ToString();
        Response.Redirect("TransferSoldier1.aspx?ID=" + ID + "&TID=" + TID);
    }
    protected void gvTransfer_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Soldier soldier = new Soldier();
            if (gvTransfer.DataKeys[e.Row.RowIndex].Values["SoldierID"] != null)
            {
                Yipusoft.SoldierArchives.Model.SoldierArchiveInfo sinfo = soldier.GetSoldierInfoByID(gvTransfer.DataKeys[e.Row.RowIndex].Values["SoldierID"].ToString());
                if (sinfo != null)
                {

                    e.Row.Cells[1].Text = soldier.GetSoldierInfoByID(gvTransfer.DataKeys[e.Row.RowIndex].Values["SoldierID"].ToString()).SoldierName;
                }
            }
            Label ID = (Label)e.Row.FindControl("ID");
            ID.Text = (gvTransfer.PageIndex * gvTransfer.PageSize + e.Row.RowIndex + 1).ToString();
            //当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色 
            e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='#F5F5F5',this.style.fontWeight='';");
            //当鼠标离开的时候 将背景颜色还原的以前的颜色 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor,this.style.fontWeight='';");

            ////查看
            //ImageButton btnview = e.Row.FindControl("btnview") as ImageButton;
            //string trid = gvTransfer.DataKeys[e.Row.RowIndex].Values["TransferID"].ToString();
            //TranslateLogInfo loginfo = TranslateLogs.GetDetailByDID(new Guid(trid));
            //if (loginfo != null)
            //{



            //    btnview.PostBackUrl = "TranslateProcess/TanslateDetail.aspx?Fid=" + loginfo.FID;
            //}
            //else
            //{
            //    btnview.Visible = false;
            //}
        }
    }
    protected void gvTransfer_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if (TransferSoldier.ClearAcceptList(gvTransfer.DataKeys[e.RowIndex].Values["TransferID"].ToString()))
        {
            BindTransfer();
        }
    }
    protected void btnAccept_Command(object sender, CommandEventArgs e)
    {
        if(e.CommandArgument==null)
            return ;
        if (string.IsNullOrEmpty(e.CommandName))
            return;

         if (string.IsNullOrEmpty(e.CommandArgument.ToString()))
            return;

         if (TransferSoldier.AcceptTransfer(e.CommandArgument.ToString(), Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).UserName, User.Identity.Name, DateTime.Now, e.CommandName))
         {
             BindAcceptCount();
             BindTransfer();
         }

    }
}
